package com.fr.data.impl.sap;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import com.fr.data.AbstractDataModel;
import com.fr.general.GeneralUtils;
import com.fr.general.data.TableDataException;
import com.fr.stable.StableUtils;

public class GeneralQuerySAPDataModel extends AbstractDataModel {
	private String connectionName;
    private String[] selectClausV6;
    private String[] fromClausV6;
    private String[] whereClausV6;
    private String[] colNames;
    private List data = null;
    private  static final int JDK5 = 5;
    private static final int FOUR = 4;
    
	public GeneralQuerySAPDataModel(String connectionName,
			String[] selectClausV6, String[] fromClausV6, String[] whereClausV6) {
		this.connectionName = connectionName;
		this.selectClausV6 = selectClausV6;
		this.fromClausV6 = fromClausV6;
		this.whereClausV6 = whereClausV6;
		dealColNames(this.selectClausV6);
	}
	
	private void dealColNames(String[] selects) {
		colNames = new String[selects.length];
		String us;
		int s = -1;
		for (int i = 0; i < selects.length; i++) {
			us = selects[i].toUpperCase();
			if (( s = us.indexOf(" AS ")) > -1) {
				colNames[i] = selects[i].substring(s + FOUR).trim();
				selects[i] = selects[i].substring(0, s).trim();
			} else if (( s = us.indexOf("~")) > -1) {
				colNames[i] = selects[i].substring(s + 1).trim();
			} else {
				colNames[i] = selects[i];
			}
		}
	}

	public int getColumnCount() throws TableDataException {
		return colNames == null ? 0 : colNames.length;
	}

	public String getColumnName(int columnIndex) throws TableDataException {
		return colNames == null ? null : colNames[columnIndex];
	}

	public int getRowCount() throws TableDataException {
		initData();
		return data.size();
	}

	public Object getValueAt(int rowIndex, int columnIndex)
			throws TableDataException {
		initData();
		return columnIndex < 0 ? null : (hasRow(rowIndex) ? ((Object[])data.get(rowIndex))[columnIndex] : null);
	}
	
	private void initData() throws TableDataException {
		if (data == null) {
			data = new ArrayList();
			
			try {
                if(StableUtils.getMajorJavaVersion() < JDK5){
				SAPUtils.extractTableData(connectionName, selectClausV6, fromClausV6, whereClausV6, data);
                }else{
                    Class SAPUtilsJDK5Class;
                    SAPUtilsJDK5Class = GeneralUtils.classForName("com.fr.data.impl.sap.SAPUtilsJDK5");
                    Method method = SAPUtilsJDK5Class.getMethod("extractTableData",new Class[]{
                        String.class,
                        String[].class,
                        String[].class,
                        String[].class,
                        List.class
                    });
                    method.invoke(SAPUtilsJDK5Class,new Object[]{
                            connectionName,
                            selectClausV6,
                            fromClausV6,
                            whereClausV6,
                            data
                    });
                }
			} catch (Exception e) {
				throw new TableDataException(e.getMessage(), e);
			}
		}
	}

	public void release() throws Exception {
		if (this.data != null) {
			this.data.clear();
			this.data = null;
		}
	}
}
